package com.fyzk.activity.lottery.zc_new;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.fyzk.bean.CurrentPhaseInfo;
import com.tyjc.lottery.R;

import java.util.List;


public class PhasePopupwindow extends PopupWindow implements View.OnTouchListener{

	private Context context;
	private List<CurrentPhaseInfo> items;
	private View view;
	private GridView gv;
	private int checkItem = 0;
	private PopItemClickListener listener;
	private HM_PopupAdapter adapter;

	@Deprecated
	public PhasePopupwindow(Context context, View view, int w, int h, List<CurrentPhaseInfo> items){
		super(view, w, h);
		this.setContentView(view);
		this.setContentView(view);
		this.context = context;
		this.view = view;
		this.items = items;
		initView();
		this.setContentView(view);
		this.setWidth(w);
		this.setHeight(h);
		this.setFocusable(true);
		this.setOutsideTouchable(true);
		// 刷新状态
		this.update();
		ColorDrawable dw = new ColorDrawable(0000000000);
		this.setBackgroundDrawable(dw);
		this.setAnimationStyle(R.style.showPopupAnimation);
	}



	@Override
	public boolean onTouch(View v, MotionEvent event) {
		this.dismiss();
		return true;
	}

	private void initView(){
		gv = (GridView) view.findViewById(R.id.hm_popu_gv);
		adapter = new HM_PopupAdapter(items);
		gv.setEnabled(false);
		gv.setAdapter(adapter);
	}
	
	
	/**
     * 显示popupWindow
     * 
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
//            this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);
        	if (null != listener) {
				listener.popupShow();
			}
        	super.showAsDropDown(parent);
        } else {
            this.dismiss();
        }
    }
    
    private void popupDismiss(){
    	this.dismiss();
    }
    
    @Override
    public void dismiss() {
    	if (null != listener) {
			listener.popupDismiss();
		}
    	super.dismiss();
    }
    
    public void setCheckItem(int checkItem){
    	this.checkItem = checkItem;
    }
    
    public int getCheckItem(){
    	return this.checkItem;
    }
    
    //===== 接口	==========
    public interface PopItemClickListener{
    	void onItem(int position);
    	void popupShow();
    	void popupDismiss();
    }
    
    public void setHm_PopupOnItemListener(PopItemClickListener listener){
    	this.listener = listener;
    }
    
    public void setItems(List<CurrentPhaseInfo> items){
    	this.items = items;
    	adapter.setItems(items);
    }
    
    //==========================================================================
	class HM_PopupAdapter extends BaseAdapter{
		private List<CurrentPhaseInfo> items;
		
		public HM_PopupAdapter(List<CurrentPhaseInfo> items){
			this.items = items;
		}

		@Override
		public int getCount() {
			
			return items.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			CurrentPhaseInfo item = items.get(position);
			convertView = LayoutInflater.from(context).inflate(R.layout.hm_popupwindow_item, null);
			TextView text = (TextView) convertView.findViewById(R.id.hm_popup_item_tv);
			
			text.setText("第"+item.getPhase()+"期");
			if (checkItem == position) {
				text.setTextColor(Color.WHITE);
				text.setBackground(context.getResources().getDrawable(R.drawable.shape_rect_e7ad00_redius));
			}else{
				text.setTextColor(context.getResources().getColor(R.color.zc_bf_left));
				text.setBackground(context.getResources().getDrawable(R.drawable.shape_rect_ffffff_redius));
			}
			
			text.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if (checkItem != position && null != listener) {
						checkItem = position;
						listener.onItem(position);
					}

					notifyDataSetChanged();
					popupDismiss();
				}
			});
			
			return convertView;
		}
		public void setItems(List<CurrentPhaseInfo> items){
			this.items = items;
			this.notifyDataSetChanged();
		}
		
	}
	
	
	
	public void setNumColumns(int numColumns){
		this.gv.setNumColumns(numColumns);
	}
	
	
	
	
	
}
